<template>
  <div 
    class="switch" 
    :class="[
      { 'switch-checked': modelValue, 'switch-disabled': disabled },
      `switch-${size}`
    ]"
    @click="toggleSwitch"
  >
    <div class="switch-handle"></div>
    <div v-if="loading" class="switch-loading">
      <div class="switch-loading-icon"></div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Switch',
  props: {
    modelValue: {
      type: Boolean,
      default: false
    },
    disabled: {
      type: Boolean,
      default: false
    },
    loading: {
      type: Boolean,
      default: false
    },
    size: {
      type: String,
      default: 'default',
      validator: (value) => {
        return ['small', 'default', 'large'].includes(value)
      }
    },
    activeColor: {
      type: String,
      default: ''
    },
    inactiveColor: {
      type: String,
      default: ''
    }
  },
  emits: ['update:modelValue', 'change'],
  computed: {
    switchStyle() {
      const style = {}
      if (this.modelValue && this.activeColor) {
        style.backgroundColor = this.activeColor
        style.borderColor = this.activeColor
      } else if (!this.modelValue && this.inactiveColor) {
        style.backgroundColor = this.inactiveColor
        style.borderColor = this.inactiveColor
      }
      return style
    }
  },
  methods: {
    toggleSwitch() {
      if (this.disabled || this.loading) {
        return
      }
      const newValue = !this.modelValue
      this.$emit('update:modelValue', newValue)
      this.$emit('change', newValue)
    }
  }
}
</script>

<style scoped>
.switch {
  position: relative;
  display: inline-block;
  width: 44px;
  height: 22px;
  border-radius: 11px;
  background-color: #bfbfbf;
  border: 1px solid #bfbfbf;
  cursor: pointer;
  transition: all 0.3s;
}

.switch-small {
  width: 28px;
  height: 16px;
  border-radius: 8px;
}

.switch-large {
  width: 56px;
  height: 28px;
  border-radius: 14px;
}

.switch-checked {
  background-color: #1890ff;
  border-color: #1890ff;
}

.switch-disabled {
  cursor: not-allowed;
  opacity: 0.4;
}

.switch-handle {
  position: absolute;
  top: 1px;
  left: 1px;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background-color: #fff;
  transition: all 0.3s;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

.switch-small .switch-handle {
  width: 12px;
  height: 12px;
  top: 1px;
  left: 1px;
}

.switch-large .switch-handle {
  width: 24px;
  height: 24px;
  top: 1px;
  left: 1px;
}

.switch-checked .switch-handle {
  left: calc(100% - 19px);
}

.switch-small.switch-checked .switch-handle {
  left: calc(100% - 13px);
}

.switch-large.switch-checked .switch-handle {
  left: calc(100% - 25px);
}

.switch-loading {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  z-index: 1;
}

.switch-loading-icon {
  width: 10px;
  height: 10px;
  border: 1px solid rgba(0, 0, 0, 0.1);
  border-top-color: #1890ff;
  border-radius: 50%;
  animation: switch-loading 1s infinite linear;
}

.switch-small .switch-loading-icon {
  width: 6px;
  height: 6px;
}

.switch-large .switch-loading-icon {
  width: 14px;
  height: 14px;
}

@keyframes switch-loading {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}
</style> 